package cn.sh.yhk.configure.filter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Map;

//@Order(1)
//@WebFilter(filterName = "PrintParamterFilter", urlPatterns = "/*")
public class PrintParamterFilter implements Filter {
    private static Logger log = LoggerFactory.getLogger(PrintParamterFilter.class);


    @Override
    public void init(javax.servlet.FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) servletRequest;
        String requestURI = req.getRequestURI();
        log.info("打印请求url:" + requestURI);
        Map<String, String[]> print = servletRequest.getParameterMap();
        log.info("开始打印传入的参数");
        for (Object key : print.keySet()) {
            if (print.keySet() != null && !"".equals(print.keySet())) {
                for (String str : print.get(key)) {
                    log.info(key + ":" + str);
                }
            } else {
                log.info(key + ":");
            }
        }
        log.info("打印参数结束");
        filterChain.doFilter(servletRequest, servletResponse);
        log.info("请求结束");
    }

    public void destroy() {
    }
}

